Ext.define('byb.view.system.AdmUserForm', {
    extend: "Ext.window.Window",
    alias: 'widget.admUserForm',
    autoShow: true,
    frame: false,
    modal: true,
    width: '765px',
    title: '新增用户',
    autoScroll: true,
    initComponent: function() {
        this.callParent(arguments);
    },
    layout: 'anchor',
    items: [{
        xtype: 'form',
        width: '745px',
        layout: 'anchor',
        frame: false,
        border: false,
        fieldDefaults: {
            labelAlign: 'right',
            allowBlank: false,
            margin: '10 20',
            msgTarget: 'side',
            labelWidth: '60px'
        },
        items: [{
            xtype: 'textfield',
            fieldLabel: '用户ID',
            name: 'id',
            allowBlank: true,
            hidden: true
        }, {
            xtype: 'textfield',
            fieldLabel: '分配ID',
            name: 'orgId',
            allowBlank: true,
            hidden: true
        }, {
            xtype: 'textfield',
            name: 'username',
            maxLength: 20,
            allowBlank: false,
            fieldLabel: '用&nbsp;户&nbsp;名',
            vtype: 'normalWithChinese'
        }, {
            anchor: '100%',
            xtype: 'container',
            layout: 'hbox',
            items: [{
                flex: 0.8,
                width: '70px',
                xtype: 'textfield',
                name: 'orgDesc',
                fieldLabel: '分配组织',
                readOnly: true
            }, {
                flex: 0.2,
                xtype: 'button',
                text: '点击修改',
                margin: '10 20 10 0'
            }]
        }, {
            anchor: '100%',
            xtype: 'container',
            layout:'hbox',
            items:[{
                width: '75px',
                xtype: 'label',
                text: '分配角色:',
                style: {
                    textAlign: 'right',
                    marginTop: '8px'
                }
            },{
            	xtype: 'container',
            	id : 'admUserMgr_assignRole',
            	style: {
            		marginLeft: '5px'
            	},
            	layout: {
            	   type: 'table',
            	   columns: 5 
            	}
            }]
        }]
    }],
    // Reset and Submit buttons
    buttons: [{
        text: '保存',
        action: "itemSave",
        id: 'itemSaveUser'
    }, {
        text: '取消',
        handler: function() {
            Ext.ComponentQuery.query('admUserForm')[0].close();
        }
    }]
    
    ,listeners : {
    	afterrender  : function( ctx, eOpts ){
    		var store = Ext.create('byb.store.system.AdmRole');
    		var records = byb.controller.SystemController.getSelectedModelRecordsForUserGrid();
    		var userId = records ? records.data.id : null; 
    		
    		if(byb.controller.SystemController.action !== 'modifyUser'){
    			userId = null;
    		}
    		
    		 Ext.Ajax.request({
                 url: byb.Constant.APIRoot+"/role/query",
                 params: {
                     id: userId 
                 },
                 method: 'GET',
                 success: function(response, opts) {
                     var obj = Ext.decode(response.responseText);
                     if (obj && obj.hasOwnProperty("results")) {
                         var fa = Ext.getCmp("admUserMgr_assignRole");
                         Ext.Array.each(obj.results, function(obj) {
                             fa.add({
                            	 xtype: 'checkboxfield',
                                 name: 'rolesForUser_checkboxgrp',
                                 boxLabel: obj.roleName,
                                 inputValue: obj.id,
                                 checked : obj.checked === 'checked' ? true : false ,
                                 margin: '5 5',
                                 width: 120
                             });
                         });
                     }
                 }
             });
        }
    }
});